/*********************************************************************************
 *      Copyright:  (C) 2017 luminqi
 *                  All rights reserved.
 *
 *       Filename:  test_button.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(2017年04月16日)
 *         Author:  luminqi <958822582@qq.com>
 *      ChangeLog:  1, Release initial version on "2017年04月16日 02时19分51秒"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>

#define KEY1 0x01
#define KEY2 0x02
#define KEY3 0x04
#define KEY4 0x08
#define PLATDRV_MAGIC   0x60
#define OFF _IO (PLATDRV_MAGIC,0x18)
#define ON _IO (PLATDRV_MAGIC,0X19)

int main (int argc, char **argv)
{
    int button_fd;
    int led_fd;
    int ret_button;
    int current_button;
    int i=0;
    int j=0;
    int x=0;
    int y=0;
    button_fd = open("/dev/button",0);

    if(button_fd<0)
    {
        printf("open button failed.\n");
        exit(1);
     }
    while(1)
    {
        ret_button = read(button_fd,&current_button,sizeof(4));
        if(ret_button != sizeof(current_button))
        {
            printf("Read() button failed.\n");
        }
        else
        {
            if (current_button == KEY1)
            { 
                led_fd = open("/dev/led0",O_RDWR,755);
                switch(i%2)
                {
                    case 0:
                        ioctl(led_fd,OFF,0);
                        i++;
                        printf("Turn led0 OFF.\n");
                        break;
                    case 1:
                        ioctl(led_fd,ON,0);
                        i++;
                        printf("Turn led0 ON.\n"); 
                        break;
                }
            }
            else if (current_button == KEY2)
            {
                led_fd = open("/dev/led1",O_RDWR,755);
                switch(j%2)
                {
                    case 0:
                        ioctl(led_fd,OFF,1);
                        j++;
                        printf("Turn led1 OFF.\n");
                        break;
                    case 1:
                        ioctl(led_fd,ON,1);
                        j++;
                        printf("Turn led1 ON.\n");
                        break;
                }
            }
            else if (current_button == KEY3)
            {
                led_fd = open("/dev/led2",O_RDWR,755);
                switch(x%2)
                {
                    case 0:
                        ioctl(led_fd,OFF,2);
                        x++;
                        printf("Turn led2 OFF.\n");
                        break;
                    case 1:
                        ioctl(led_fd,ON,2);
                        x++;
                        printf("Turn led2 ON.\n");
                    break;

                }
            }
            else if (current_button == KEY4)
            {
                led_fd = open("/dev/led3",O_RDWR,755);
                switch(y%2)
                {
                    case 0:
                        ioctl(led_fd,OFF,3);
                        y++;
                        printf("Turn led3 OFF.\n");
                        break;
                    case 1:
                        ioctl(led_fd,ON,3);
                        y++;
                        printf("Turn led3 ON.\n");
                         break;
                }
            }
        }
    }
    ioctl(led_fd,OFF);
    close(led_fd);
    close(button_fd);
    return 0;
}

